package org.myOrg.spetrum.Listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import org.myOrg.spetrum.io.InputManager;
import org.myOrg.spetrum.util.Message;
import org.myOrg.spetrum.util.MessageImplement;
import org.myOrg.spetrum.util.MessageMangement;
import org.myOrg.spetrum.util.MessageMangementImplement;
import org.myOrg.spetrum.util.OverallFactory;
import org.myOrg.spetrum.util.Reciver;
import org.myOrg.spetrum.util.Sender;

//import javax.swing.JPanel;

public class AddBugModeListener implements ActionListener, Sender, Reciver {

	private int[] param;
	private int[] temp;
	private MessageMangement mangerment;

	public AddBugModeListener() {
		this.setMessageMangement(OverallFactory.getMessageManger());
		this.register(MessageMangementImplement.ADDTEMPVALUE,
				OverallFactory.getMessageManger());
		this.register(MessageMangementImplement.READOVER,
				OverallFactory.getMessageManger());
	}

	public void resetTemp() {
		temp = new int[param.length];
		for (int i = 0; i < temp.length; i++) {
			temp[i] = -1;
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if (e.getActionCommand().equals("done")) {
			Message msg = new MessageImplement(this,
					MessageMangementImplement.ADDBUGMODEL, temp);
			this.sendMessage(msg);
			this.resetTemp();
		}
	}

	@Override
	public void sendMessage(Message msg) {
		// TODO Auto-generated method stub
		mangerment.dispatchMeesage(msg);
	}

	@Override
	public void setMessageMangement(MessageMangement mangerment) {
		// TODO Auto-generated method stub
		this.mangerment = mangerment;
	}

	@Override
	public void register(int messageType, MessageMangement messageManger) {
		// TODO Auto-generated method stub
		messageManger.register(this, messageType);
	}

	@Override
	public void handleMessage(Message msg) {
		// TODO Auto-generated method stub
		if (msg.getType() == MessageMangementImplement.ADDTEMPVALUE) {
			int[] info = (int[]) msg.getInfo();
			temp[info[0]] = info[1];
		}
		else if(msg.getType() == MessageMangementImplement.READOVER){
			InputManager input = (InputManager) msg.getInfo();
			if(input.isFormatPass()){
				this.param = input.getParam();
				this.resetTemp();
			}
		}

	}

}
